package org.gp953.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.gp953.basic.exception.BusinessException;
import org.gp953.basic.util.AjaxResult;
import org.gp953.basic.util.PageList;
import org.gp953.user.domain.User;
import org.gp953.user.dto.UserDto;
import org.gp953.user.query.UserQuery;
import org.gp953.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController // 复合注解相当于 @Controller + @ResponseBody
@RequestMapping("/user")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 通过id查询一个
     * @param id
     * @return User
     */
    @GetMapping("/{id}")// {id}是占位符，@PathVariable表示从url的中拿值
    public User queryById(@PathVariable("id") Long id){
        return userService.queryById(id);
    }


    /**
     * 查询所有
     * @return
     */
    @GetMapping// {id}是占位符，@PathVariable表示从url的中拿值
    public List<User> queryAll(){
        return userService.queryAll();
    }

    /**
     * 添加和修改
     * @param user
     * @return
     */
    @ApiOperation(value = "用户添加或修改",notes = "如果有id就是修改否则就是添加")
    @PutMapping// @RequestBody表示从请求体中拿值
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if (user.getId()!=null){
                userService.update(user);
            }else {
                userService.add(user);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！！");
        }
    }

    /**
     *  通过id删除
     * @return
     * 定义接口的三要素：
     * 1.请求地址
     * 2.请求参数
     * 3.返回值
     */
    @DeleteMapping("/{id}")// {id}是占位符，@PathVariable表示从url的中拿值
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            userService.delete(id);

            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败").setSuccess(false);
        }
    }


    /**
     *
     * 高级查询
     * @param query
     * @return
     */
    @PostMapping// @RequestBody表示从请求体中拿值
    public PageList<User> queryPage(@RequestBody UserQuery query){
        return userService.queryPageData(query);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping("/user")// @PatchMapping批量操作的注解
    // List<Long> ids 和Long[] ids 都可以接受数据，但在mapper映射文件中foreach标签要注意区分，使用list还是array
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            userService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败").setSuccess(false);
        }
    }

    @PostMapping("/phone/register")
    public AjaxResult phoneRegister(@RequestBody UserDto userDto){
        try {
            userService.phoneRegister(userDto);
            return AjaxResult.me();
        }catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("哦豁，系统崩溃了，我们正在殴打程序员").setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败").setSuccess(false);
        }
    }

}
